package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTMachineFaultDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.SignalMachineFaultDto;
import com.hualude.roadsign.model.MachineFaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by pjn on 2017/7/22.
 */
public interface SignalMachineFaultRepository extends CrudRepository<MachineFaultEntity, Long> {
    /**
     * @Description: 获取信号机datatable
     * @Author: pan-jianan
     * @Date: 2017/7/22 17:20
     */
    public Integer getMachineReportFaultCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public List<DTMachineFaultDto> getMachineReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;


    /**
     * @Description: 信号机故障详情
     * @Author: pan-jianan
     * @Date: 2017/7/22 18:47
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalMachineFaultDto(sm.machineLevel,sm.machinePhoto,sm.machineType," +
            "sm.machineReason,sm.machineCreatorId,sm.machineTime,sm.machineCheck,sm.machineMainCheck,sm.machineFeedback," +
            "sm.machineMoney) " +
            "from MachineFaultEntity sm where sm.machineFaultId=?1")
    public SignalMachineFaultDto getSignalMachineFaultInfo2Fault(Integer machineFaultId) throws Exception;

    /**
     * @Description: 更新故障
     * @Author: pan-jianan
     * @Date: 2017/7/22 20:58
     */
    @Modifying
    @Query(value = "UPDATE MachineFaultEntity  mf SET mf.machineLevel = ?2,mf.machineType = ?3,mf.machinePhoto = ?4,mf.machineReason = ?5,mf.machineMoney =?6 WHERE mf.machineFaultId =?1")
    public Integer updateSignalMachineFault(Integer machineFaultId, Integer machineLevel, Integer machineType, String machinePhoto, String machineReason, Double machineMoney) throws Exception;

    /**
     * @Description: 信号机故障审核
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:18
     */
    public List<DTMachineFaultDto> getSignalMachineFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    public Integer getSignalMachineFaultCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception;

    /**
     * @Description: 主管审核通过
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:18
     */
    @Modifying
    @Query(value = "UPDATE MachineFaultEntity  mf SET mf.machineMainCheck= ?1,mf.machineMainCheckerId=?4,mf.machineFeedback=?2,mf.machineMaintenanceStatus=1,mf.machineMainCheckTime=?5 WHERE mf.machineFaultId=?3")
    public Integer machineFaultAudit2Success(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 主管审核拒绝
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:19
     */
    @Modifying
    @Query(value = "UPDATE MachineFaultEntity  mf SET mf.machineMainCheck= ?1,mf.machineMainCheckerId=?4,mf.machineFeedback=?2,mf.machineMainCheckTime=?5 WHERE mf.machineFaultId=?3")
    public Integer machineFaultAudit2Fail(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 故障处理DT
     * @Author: jiang-weirong
     * @Date: 2017/7/22 18:53
     */
    public List<DTMachineFaultDto> getSignalMachine4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception;

    public Integer getSignalMachineCount4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception;

    /**
     * @Description: 施工方案
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:42
     */
    @Modifying
    @Query(value = "UPDATE MachineFaultEntity  mf SET mf.machineMaintenanceContent = ?2 WHERE mf.machineFaultId =?1")
    public Integer addMachineMaintenanceContent(Integer machineFaultId, String machineMaintenanceContent) throws Exception;

    /**
     * @Description: 完成故障维修
     * @Author: jiang-weirong
     * @Date: 2017/7/15 13:43
     */
    @Modifying
    @Query(value = "UPDATE MachineFaultEntity  mf SET mf.machineMaintenancePhoto = ?1,mf.machineMaintainerId=?2,mf.machineMaintenanceTime=?3,mf.machineMaintenanceExplain=?4,mf.machineMaintenanceStatus=2 WHERE mf.machineFaultId =?5")
    public Integer finishMachineMaintenance(String machineMaintenancePhoto, Integer machineMaintainerId, String machineMaintenanceTime, String machineMaintenanceExplain, Integer machineFaultId);

    /**
     * @Description: 获取企业负责人审核情况
     * @Author: jiang-weirong
     * @Date: 2017/7/22 20:35
     */
    @Query(value = "select mf.machineCheck from MachineFaultEntity mf where mf.machineFaultId = ?1")
    public Integer getMachineCheck(Integer machineFaultId) throws Exception;

    /**
     * @Description: 企业负责人审核
     * @Author: jiang-weirong
     * @Date: 2017/7/22 20:36
     */
    @Modifying
    @Query(value = "UPDATE MachineFaultEntity  mf SET mf.machineCheck= ?1,mf.machineCheckerId=?4,mf.machineFeedback=?2,mf.machineCheckTime=?5 WHERE mf.machineFaultId=?3")
    public Integer machineFaultAudit(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr) throws Exception;

    /**
     * @Description: 删除信号机故障
     * @Author: pan-jianan
     * @Date: 2017/7/22 21:38
     */
    @Modifying
    @Query(value = "delete from MachineFaultEntity mf where mf.machineFaultId=?1")
    public Integer deleteMachineFaultByIds(Integer machineFaultId) throws Exception;

    /**
     * @Description: 删除信号机故障
     * @Author: xu_jiaHui
     * @Date: 2017/7/26 13:05
     */
    @Modifying
    @Query(value = "delete from MachineFaultEntity where signalMachineId=?1")
    public Integer deleteMachineFault(Integer signalMachId) throws Exception;

    /**
     * @Description: 判断是否可上报
     * @Author: jiang-weirong
     * @Date: 2017/7/14 17:16
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalMachineFaultDto(mf.machineCheck,mf.machineMainCheck,mf.machineMaintenanceStatus) " +
            "from MachineFaultEntity mf where mf.signalMachineId=?1  ORDER BY mf.machineTime DESC ")
    public List<SignalMachineFaultDto> getMachineFaultIsReport(Integer signalMachineId) throws Exception;


    /**
     * @Description: 获取信号机审核列表4App
     * @Author: pan-jianan
     * @Date: 2017/7/17 16:18
     */
    public List<SignalMachineFaultDto> getMachineFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus,
                                                                    Integer count, Integer size) throws Exception;

    /**
     * @Description: app获取信号机故障维修列表
     * @Author: pan-jianan
     * @Date: 2017/7/20 14:22
     */
    public List<SignalMachineFaultDto> getMachineFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size)throws Exception;

    /**
     * @Description: app 回执单
     * @Author: pan-jianan
     * @Date: 2017/7/26 15:07
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalMachineFaultDto(sm.signalMachineId,mf.machineLevel, mf.machinePhoto," +
            "mf.machineType,mf.machineReason,mf.machineMainCheckTime,sm.signalMachineLongitude,sm.signalMachineLatitude,sm.signalMachineProvince," +
            "sm.signalMachineCity, sm.signalMachineArea, sm.signalMachineStartRoad, sm.signalMachineEndRoad, mf.machineMaintenancePhoto," +
            " mf.machineMaintenanceContent, mf.machineMaintenanceExplain,mf.machineMaintenanceTime,mf.machineFeedback)" +
            " from SignalMachineEntity sm,MachineFaultEntity mf " +
            "WHERE sm.signalMachineId=mf.signalMachineId AND mf.machineFaultId = ?1")
    public SignalMachineFaultDto getMachineReceiptInfo4App(Integer machineFaultId) throws Exception;

    public List<SignalMachineFaultDto> getMachineFaultList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception;

    /**
     * @Description: 获取推送相关信息
     * @Author: jiang-weirong
     * @Date: 2017/9/11 10:56
     */
    @Query(value = "select new com.hualude.roadsign.dto.SignalMachineFaultDto(signalMachineId, machineType, machineOrgId, machineMainOrgId) from MachineFaultEntity where machineFaultId = ?1")
    public SignalMachineFaultDto getInfoByFaultId(Integer machineFaultId) throws Exception;

    @Modifying
    @Query(value = "UPDATE MachineFaultEntity  lf SET lf.machineCheck= ?1,lf.machineCheckerId=?4,lf.machineFeedback=?2,lf.machineCheckTime=?5,lf.machineMainCheck=?1,lf.machineMainCheckTime=?5,lf.machineMaintenanceStatus=1 WHERE lf.machineFaultId=?3")
    public Integer straightFaultAudit(Integer isPass, String feedback, Integer machineFaultId, Integer userId, String dateNowStr);
}
